﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using nelech.Models.DBModel;
using nelech.Views.Shared.Code;
using nelech.Controllers.Utils;

namespace nelech.Controllers
{
    public partial class PlaceController : Controller
    {
        nelechDBEntities db = new nelechDBEntities();
        //
        // GET: /Place/
        public ActionResult Index()
        {
            var places = db.Place.Where(p => p.publish == 1).ToList();
            places = places.OrderBy(p => p.id, new ReverseIntComparer()).ToList();
            if (places.Count() > 5)
                places = places.GetRange(0, 5).ToList();
            var recommended = db.RecommendedPlace.ToList();
            var recommendedPlace = new List<Place>();
            foreach (var recPlace in recommended)
            {
                var place = db.Place.First(p => p.id == recPlace.id);
                recommendedPlace.Add(place);
                LoadPlaceRelations(place);
                place.Image.OrderBy(v => v.path);
            }
            foreach (var place in places)
            {
                LoadPlaceRelations(place);
            }

            var placeArry = new Object[2];
            placeArry[0] = places;
            placeArry[1] = recommendedPlace;
            return View(placeArry);

        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Search(int regeon, int cost, int type, string searchString)
        {
            var adverts = db.Advert.Where(p => p.publish.HasValue && p.publish.Value == 1);
            int advertCount = Utils.AdvertControllerUtils.GetAdvertCount(6, null, db);
            var advertsArray = Utils.AdvertControllerUtils.GetUniqueArray(advertCount);
            ViewData["advertsArray"] = advertsArray;
            ViewData["advertCount"] = advertsArray == null ? 0 : advertsArray.Length;
            var places = db.Place.Where(p => p.publish == 1).ToList();
            if (regeon != 21)
            {
                places = places.Where(p => { p.Regeon1Reference.Load(); return p.Regeon1.id == regeon; }).ToList();
            }
            if (type != 1)
            {
                var typeMatch = new List<Place>();
                foreach (var place in places)
                {
                    place.Type.Load();
                    if (place.Type.Select(s => s.id).Contains(type))
                    {
                        typeMatch.Add(place);
                    }
                }
                places = typeMatch;
            }
            if (cost != 1)
            {
                places = places.Where(p => { p.Cost1Reference.Load(); return p.Cost1.id == cost; }).ToList();
            }

            places = SharedUtils.FreeSearchPlace(places, searchString);
            var listPlace = new ListPlace() { places = places, type = "", isEmpty = false, search = true };
            if (places.Count() > 0)
            {

                foreach (var place in places)
                {
                    LoadPlaceRelations(place);
                    place.Image.OrderBy(p => p.path);
                }
            }
            else
            {
                listPlace.isEmpty = true;
            }

            return View(listPlace);
        }

        public ActionResult PlaceInfo(int id)
        {
            var place = db.Place.ToList().First(p => p.id == id);
            LoadPlaceRelations(place);
            var lon = place.lon;
            var lat = place.lat;
            var nearPlaces = db.Place.AsEnumerable().Where(p => p.publish == 1 && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2) && p.id != id).AsEnumerable().OrderBy(p => p, new NearPlaceComparer(lon, lat)).Take(4);
            var nearSprings = db.Maayan.AsEnumerable().Where(p => p.publish == 1 && SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2).AsEnumerable().OrderBy(p => p, new NearMaayanComparer(lon, lat)).Take(4);
            var nearTracks = db.Track.AsEnumerable().Where(p => p.publish == 1 && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2) ).AsEnumerable().OrderBy(p => p, new NearTrackComparer(lon, lat)).Take(4);
            var nearZimers = db.Zimer.AsEnumerable().Where(p => p.publish == 1 && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2)).AsEnumerable().OrderBy(p => p, new NearZimerComparer(lon, lat)).Take(4);
            var nearAttrs = db.Attraction.AsEnumerable().Where(p => p.publish == true && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2)).AsEnumerable().OrderBy(p => p, new NearAttrComparer(lon, lat)).Take(4);
            var adverts = db.Advert.Where(p => p.publish.HasValue && p.publish.Value == 1);
            int advertCount = Utils.AdvertControllerUtils.GetAdvertCount(6, place.regeon, db);
            var advertsArray = Utils.AdvertControllerUtils.GetUniqueArray(advertCount);
            ViewData["advertsArray"] = advertsArray;
            ViewData["advertCount"] = advertsArray == null ? 0 : advertsArray.Length;
            var nearLinks = new NearLinks()
            {
                nearPlaces = nearPlaces.ToList(),
                nearSprings = nearSprings.ToList(),
                nearTracks = nearTracks.ToList(),
                nearZimers = nearZimers.ToList(),
                nearAttractions = nearAttrs.ToList()
            };

            ViewData["FeedBack"] = new Feedbacks() { type = 2, parentId = id };
            ViewData["ListFeedbacks"] = db.Feedback.Where(p => p.parent_id == id && p.FeedbackType.id == 2);
           
            ViewData["NearLinks"] = nearLinks;
            return View(place);
        }

        public ActionResult SpringInfo(int id)
        {
            var spring = db.Maayan.First(p => p.id == id);
            LoadSpringRelations(spring);
            var lon = spring.lon;
            var lat = spring.lat;
            var nearPlaces = db.Place.AsEnumerable().Where(p => p.publish == 1 && SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2).OrderBy(p => p, new NearPlaceComparer(lon, lat)).Take(4);
            var nearSprings = db.Maayan.AsEnumerable().Where(p => p.publish == 1 && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2) && p.id != id).OrderBy(p => p, new NearMaayanComparer(lon, lat)).Take(4);
            var nearTracks = db.Track.AsEnumerable().OrderBy(p => p.publish == 1 && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2)).OrderBy(p => p, new NearTrackComparer(lon, lat)).Take(4);
            var nearZimers = db.Zimer.AsEnumerable().OrderBy(p => p.publish == 1 && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2)).OrderBy(p => p, new NearZimerComparer(lon, lat)).Take(4);
            var nearAttrs = db.Attraction.AsEnumerable().Where(p => p.publish == true && (SharedUtils.Distance(lon, lat, p.lon, p.lat) <= 0.2)).AsEnumerable().OrderBy(p => p, new NearAttrComparer(lon, lat)).Take(4);
          
            var adverts = db.Advert.Where(p => p.publish.HasValue && p.publish.Value == 1);
            int advertCount = Utils.AdvertControllerUtils.GetAdvertCount(6, spring.regeon, db);
            var advertsArray = Utils.AdvertControllerUtils.GetUniqueArray(advertCount);
            ViewData["advertsArray"] = advertsArray;
            ViewData["advertCount"] = advertsArray == null ? 0 : advertsArray.Length;
            var nearLinks = new NearLinks()
            {
                nearPlaces =  nearPlaces.ToList(), 
                nearSprings =  nearSprings.ToList(),
                nearTracks =  nearTracks.ToList(),
                nearZimers = nearZimers.ToList(),
                nearAttractions = nearAttrs.ToList()
            };
            ViewData["NearLinks"] = nearLinks;
            ViewData["FeedBack"] = new Feedbacks() { type = 3, parentId = id };
            ViewData["ListFeedbacks"] = db.Feedback.Where(p => p.parent_id == id && p.FeedbackType.id == 3);
            return View(spring);
        }





        public ActionResult ListSpring()
        {
            var springs = db.Maayan.Where(p => p.publish == 1).ToList();
            springs = springs.OrderBy(p => p.id, new ReverseIntComparer()).ToList();
            if (springs.Count() > 0)
            {
                foreach (var spring in springs)
                {
                    LoadSpringRelations(spring);
                    spring.Image.OrderBy(p => p.path);
                }
            }
            var listSpring = new ListSpring() { listMaayan = springs, isEmpty = false };
            return View(listSpring);
        }

        public ActionResult ListSpecialPlace(int id)
        {
            var places = db.Place.Where(p => p.publish == 1).ToList();
            var specialPlaces = new List<Place>();
            foreach (var place in places)
            {
                place.Type.Load();
                if (place.Type.Any(p => p.id == id))
                    specialPlaces.Add(place);
            }
            specialPlaces = specialPlaces.OrderBy(p => p.id, new ReverseIntComparer()).ToList();
            var listPlace = new ListPlace() { places = specialPlaces, type = PluralType(id), isEmpty = true, search = false };
            if (specialPlaces.Count() > 0)
            {
                foreach (var place in specialPlaces)
                {
                    LoadPlaceRelations(place);
                    place.Image.OrderBy(p => p.path);
                }
                listPlace.isEmpty = false;
            }
            return View("Search", listPlace);
        }

        private static String PluralType(int id)
        {
            switch (id)
            {
                case 2:
                    return ("קברים");

                case 3:
                    return ("אתרים היסטוריים");

                case 4:
                    return ("מסעדות");

                case 5:
                    return ("מוזיאונים");

                case 6:
                    return ("בתי כנסת");

                case 7:
                    return ("פארקים עירוניים");

                case 8:
                default:
                    return ("מרכז מבקרים");

            }
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SearchSpring(int regeon, int nevia, string searchString)
        {
            var adverts = db.Advert.Where(p => p.publish.HasValue && p.publish.Value == 1);
            int advertCount = Utils.AdvertControllerUtils.GetAdvertCount(6, null, db);
            var advertsArray = Utils.AdvertControllerUtils.GetUniqueArray(advertCount);
            ViewData["advertsArray"] = advertsArray;
            ViewData["advertCount"] = advertsArray == null ? 0 : advertsArray.Length;
            var springs = db.Maayan.Where(p => p.publish == 1).ToList();
            if (regeon != 21)
            {
                springs = springs.Where(p => { p.Regeon1Reference.Load(); return p.Regeon1.id == regeon; }).ToList();
            }
            if (nevia != 1)
                springs = springs.Where(p => { p.Nevia1Reference.Load(); return p.Nevia1.id == nevia; }).ToList();

            springs = SharedUtils.FreeSearchSpring(springs, searchString).ToList();
            var listSpring = new ListSpring() { listMaayan = springs, isEmpty = false };
            if (springs.Count() > 0)
            {

                foreach (var spring in listSpring.listMaayan)
                {
                    LoadSpringRelations(spring);
                    spring.Image.OrderBy(p => p.path);
                }
            }
            else
            {
                listSpring.isEmpty = true;
            }
            return View(listSpring);
        }

       
    }
}
